package com.extension.company.view.home.hotactivity;

import android.graphics.Rect;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.extension.company.admin.R;
import com.extension.company.common.mvp.BaseTitleActivity;
import com.extension.company.contract.home.OnlinePaySuccessContract;
import com.extension.company.eventbus.OrderId;
import com.extension.company.model.entity.RetailGoosListBean;
import com.extension.company.presenter.home.OnlinePaySuccessActivityPresenter;
import com.extension.company.view.home.adapter.PaySuccessAdapter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 支付成功的界面
 * 这里功能比较少，直接使用mvc的模式
 */
public class PaySuccessActivity extends BaseTitleActivity<OnlinePaySuccessContract.View,
		OnlinePaySuccessActivityPresenter> implements OnlinePaySuccessContract.View {
	@BindView(R.id.pay_success_recycleviee)
	RecyclerView recyclerView;
	@BindView(R.id.pay_money)
	TextView payMoneyTv;
	private List<RetailGoosListBean.DataBean> list;
	private PaySuccessAdapter adapter;

	@Override
	public int getMainLayout() {
		return R.layout.activity_pay_success;
	}

	@Override
	public OnlinePaySuccessContract.View createView() {
		return this;
	}

	@Override
	public OnlinePaySuccessActivityPresenter createPresenter() {
		return new OnlinePaySuccessActivityPresenter();
	}


	@Override
	public void initData() {
		setTitle("支付成功");
		initView();
		if (!EventBus.getDefault().isRegistered(this)) {
			EventBus.getDefault().register(this);
		}
		presenter.retaiGoodslList(); // 请求分销的列表
	}

	private void initView() {
		list = new ArrayList<>();
		recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
		recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
			@Override
			public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
				super.getItemOffsets(outRect, view, parent, state);
				outRect.top=30;
			}
		});
		adapter = new PaySuccessAdapter(this, list);
		recyclerView.setAdapter(adapter);
	}

	@Override
	public void initListener() {

	}


	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (EventBus.getDefault().isRegistered(this)) {
			EventBus.getDefault().unregister(this);
		}
	}

	@Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
	public void onMessageEvent(OrderId orderId) {
		presenter.commitOrder(orderId.getOrderId()); // 发送网络请求，确认支付订单
	}


	@Override
	public void showLoading() {

	}

	@Override
	public void hideLoading() {

	}

	@Override
	public void onError(Throwable throwable) {

	}

	@Override
	public void commitOrderSuccess(double totalPrice) { // 确认订单返回的支付金额
		payMoneyTv.setText(String.format(getResources().getString(R.string.pay_money_num), totalPrice));

	}

	@Override
	public void retailListSuccess(List<RetailGoosListBean.DataBean> retailList) {
		if (null != list) {
			list.clear();
		}
		if (null != retailList && null != list) {
			list.addAll(retailList);
			adapter.notifyDataSetChanged();
		}
	}
}
